#include <QLabel>
#include <QMouseEvent>
#include "titlebar.h"
#include "controls/button.h"

TitleBar::TitleBar(QWidget *parent) :
    QWidget(parent)
{
    initCommonControls();
    m_menuBtn = new Button(":/images/Menu.png", 4, this);
    m_skinBtn = new Button(":/images/Skin.png", 4, this);
    m_updateBtn = new Button(":/images/update_btn.png", 4, this);

    // 信号转发
    connect(m_menuBtn, &Button::clicked, this, &TitleBar::showMenu);
    connect(m_skinBtn, &Button::clicked, this, &TitleBar::skinChange);
    connect(m_updateBtn, &Button::clicked, this, &TitleBar::updateVersion);

    initToolBarLayout();
}

TitleBar::TitleBar(bool simple, QWidget *parent)
{
    initCommonControls();
    initToolBarLayout(true);
}

void TitleBar::insertUserLayout(QLayout *UserLayout, int pos, int stretch)
{
    m_layout->insertLayout(pos, UserLayout, stretch);
}

void TitleBar::insertUserWidget(QWidget *widget, int index, int stretch, Qt::Alignment alignment)
{
    m_layout->insertWidget(index, widget, stretch, alignment);
}

void TitleBar::setTitleName(QString name)
{
    m_titleName->setText(name);
}

void TitleBar::setMainWindow(QWidget *parent)
{
    m_parent = parent;
}

void TitleBar::setMoveable(bool isMove)
{
    m_isMove = isMove;
}

void TitleBar::mousePressEvent(QMouseEvent *event)
{
    if(m_isMove)
    {
        // 如果是左键
        if(event->button() == Qt::LeftButton)
        {
            m_pt = event->globalPos() - m_parent->geometry().topLeft();
        }
        return;
    }
    event->ignore();
}

void TitleBar::mouseMoveEvent(QMouseEvent *event)
{
    if(m_isMove)
    {
        // 左键移动窗口
        if(event->buttons() & Qt::LeftButton)
        {
            // 主窗口移动
            m_parent->move(event->globalPos() - m_pt);
        }
        return;
    }
    event->ignore();
}

void TitleBar::initCommonControls()
{
    m_closeBtn = new Button(":/images/sys_button_close.png", 4, this);
    m_minimizeBtn =new Button(":/images/min.png", 4, this);
    // 信号转发
    connect(m_closeBtn, &Button::clicked, this, &TitleBar::closeWin);
    connect(m_minimizeBtn, &Button::clicked, this, &TitleBar::minimumWin);
}

void TitleBar::initToolBarLayout(bool isSimple)
{
    m_layout = new QHBoxLayout;

    // logo标签
    QLabel *logo = new QLabel(this);
    logo->setPixmap(QPixmap(":/images/360logo.png"));
    m_titleName = new QLabel("360安全卫士11");
    // 将标签添加到布局中
    m_layout->addSpacing(10);
    m_layout->addWidget(logo);
    m_layout->addSpacing(6);
    m_layout->addWidget(m_titleName);
    if(!isSimple)
    {
        m_layout->addWidget(m_updateBtn);
    }

    m_layout->addStretch(0);
    if(!isSimple)
    {
        m_layout->addWidget(m_skinBtn, 0);
        m_layout->addWidget(m_menuBtn, 0);
    }
    m_layout->addWidget(m_minimizeBtn, 0);
    m_layout->addWidget(m_closeBtn, 0);
    m_layout->setSpacing(3);

    m_layout->setContentsMargins(0, 0, 0, 0);
    setLayout(m_layout);

    // 根据布局得到的最佳大小设置当前TitleBar的高度
    this->setFixedHeight(m_layout->sizeHint().height());
}
